package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealPage;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Envy
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    public R<SetmealPage> pageQuery(String name, Integer page, Integer pageSize) {
        Integer pageMin = (page - 1) * pageSize;
        return setmealService.pageQuery(name, pageMin, pageSize);
    }

    /**
     * 删除套餐内容
     */
    @DeleteMapping()
    public R delete(@RequestParam("ids") List<String> ids) {
        Boolean result = setmealService.delete(ids);
        if (result){
            return R.success("删除成功");
        }else {
            return R.error("套餐正在售卖,不能删除");
        }
    }

    /**
     * 新增套餐
     */
    @PostMapping()
    public R addSetmeal(@RequestBody Setmeal setmeal) {
        return setmealService.addSetmeal(setmeal);
    }


    /**
     * 根据id获取套餐
     */
    @GetMapping("/{id}")
    public R selectSetmeal(@PathVariable String id) {
        return setmealService.selectSetmeal(id);
    }

    /**
     * 修改套餐
     */
    @PutMapping()
    public R updeteSetmeal(@RequestBody Setmeal setmeal) {
        return setmealService.updeteSetmeal(setmeal);
    }

    /**
     * 套餐上架/下架
     */
    @PostMapping("/status/{status}")
    public R updatestatus(@RequestParam List<String> ids, @PathVariable Integer status) {
        return setmealService.updatestatus(ids, status);
    }
}
